package com.fan.ozlotte;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

public class OZLotte {
	
	private HashMap<String, Integer> map = new HashMap<String, Integer>();
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		OZLotte ozLotte = new OZLotte();
		String[][] data = ozLotte.readData("e:\\temp\\ozlotte.csv");
		ozLotte.buildMap(45);
		ozLotte.constructMap(data);
		
		ArrayList<Pair> pairs = ozLotte.getPairs();
		
		Collections.sort(pairs);
//		System.out.println(pairs.size());
		
		for(int i = 0; i < pairs.size(); i++)
		{
			System.out.println(pairs.get(i).toString() + " ");
		}
		
		
	}

	private String[][] readData(String fileName) {
		String[][] data = new String[974][7];
		int index = 0;
		try {
			File file = new File(fileName);
			BufferedReader reader = new BufferedReader(new FileReader(file));
			String line = reader.readLine();
			while (line != null) {
				String[] lineData = line.split(",");

//				System.out.println(index);
				data[index] = lineData;
				line = reader.readLine();
				index++;
			}

//			System.out.println(data.length);

			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return data;
	}


	private void buildMap(int size)
	{
		for(int i = 1; i <= size; i++ )
		{
			map.put(String.valueOf(i), 0);
		}
	}
	
	private void constructMap(String[][] data)
	{
		for(int i = 0; i < data.length; i++)
		{
			for(int j = 0; j < data[i].length; j++)
			{
				String key = data[i][j];
				if(map.containsKey(key))
				{
					int value = map.get(key);
					value++;
					map.put(key, value);
				}
				
			}
		}
		
	}
	
	private ArrayList<Pair> getPairs()
	{
		ArrayList<Pair> pairs = new ArrayList<Pair>();
		
		Iterator<Entry<String, Integer>> iterator = map.entrySet().iterator();
		while(iterator.hasNext())
		{
			Entry<String, Integer> entry = iterator.next();
			Pair pair = new Pair(entry.getKey(), entry.getValue());
			pairs.add(pair);
		}
		
		return pairs;
	}
	
	
}

class Pair implements Comparable<Pair>
{
	private String value;
	private int count;
	public Pair(String value, int count) {
		super();
		this.value = value;
		this.count = count;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	@Override
	public boolean equals(Object obj)
	{
		if(count == ((Pair)obj).getCount())
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	@Override
	public String toString()
	{
		return value + " " + count;
	}
	public int compareTo(Pair pair) {
		if(count > pair.getCount())
		{
			return 1;
		}
		else if(count == pair.getCount())
		{
			return 0;
		}
		else
		{
			return -1;
		}
	}
}
